<script setup lang="ts">
import Authorization from '@utils/authorization'
import settings from '@/settings'
import qs from 'qs'
import { ServerEnum } from '@/api/enum/server'

const props = withDefaults(defineProps<{
  // 请求地址
  url: string,
  // 下载的文件名
  fileName?: string,
  // 服务地址
  server: ServerEnum,
  // 携带的参数
  params?: Record<string, any>
}>(), {
  fileName: '导出资源',
  server: ServerEnum.System
})

onMounted(() => {
  updateDownloadHref()
})

// region TODO 下载地址
const downloadHref = shallowRef('')
function updateDownloadHref () {
  const params = { token: Authorization.token, ...props.params || {} }
  downloadHref.value = `${settings.URL.BASE_URL}${props.server}${props.url}?${qs.stringify(params)}`
}

watch(() => props.params, () => {
  updateDownloadHref()
})
// endregion
</script>

<template>
  <a :download="props.fileName" :href="downloadHref" target="_blank">
    <slot name="default" />
  </a>
</template>

<style scoped lang="scss">

</style>
